package it.dgtest.client.crud;

import it.dgtest.client.datasource.ProvinceDataSource;
import it.dgtest.shared.Comune;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.oa.gform.client.GFormBinder;
import com.oa.gform.client.bean.binder.BeanBinder;
import com.oa.gform.client.service.GFormSuccessAsyncCallback;
import com.oa.gform.client.service.ibatis.GIBatisProxyService;
import com.oa.gform.client.service.ibatis.GIBatisProxyServiceAsync;
import com.oa.gform.client.ui.GFieldHidden;
import com.oa.gform.client.ui.GFieldLabel;
import com.oa.gform.client.ui.GFieldListBox;
import com.oa.gform.client.ui.GFieldTextBox;
import com.oa.gform.client.ui.GForm;
import com.oa.gform.client.validator.GValidationCallbackSuccess;
import com.oa.gform.rebind.GBeanBinder;
import com.oa.gform.rebind.GDataSource;
import com.oa.gform.rebind.GValidators;
import com.oa.gform.rebind.GBeanBinder.BindMode;
import com.oa.gform.rebind.GDataSource.LoadOn;
/***
 * Autogenerato con DecGen.
 * @author Marco
 *
 */
public class CrudComune extends GForm {
	private static CrudComuneUiBinder uiBinder = GWT.create(CrudComuneUiBinder.class);
	interface CrudComuneUiBinder extends UiBinder<Widget, CrudComune> {}

	private static CrudComuneGFormBinder gFormBinder = GWT.create(CrudComuneGFormBinder.class);
	interface CrudComuneGFormBinder extends GFormBinder<Widget, CrudComune> {}

	@UiField GFieldLabel lblNome, lblIdProvincia , lblCap;
	
	@UiField 
	@GValidators(GFieldLabelName="lblNome", NotEmpty=true) 
	GFieldTextBox<String> fldNome;
	
	@UiField 
	@GValidators(GFieldLabelName="lblIdProvincia", NotEmpty=true,Pattern="[0-9][A-Z]") 
	@GDataSource(dataSource=ProvinceDataSource.class,loadingMode = LoadOn.CREATE) 
	GFieldListBox<Long> fldIdProvincia;

	@UiField 
	@GValidators(GFieldLabelName="lblCap", NotEmpty=true,Pattern="[0-9]") 
	GFieldTextBox<String> fldCap;
	
	GFieldHidden<Long> fldIdComune = new GFieldHidden<Long>();

	Comune comune = new Comune();
	
	@GBeanBinder(mode=BindMode.AUTO,beanInstanceName = "comune")
	BeanBinder<CrudComune, Comune> comuneBinder;
	
	GIBatisProxyServiceAsync<Comune> service = GWT.create(GIBatisProxyService.class);
	
	/***
	 * Costruttore della form
	 */
	public CrudComune() {
		// Inizializza il tamplate ui.xml
		initWidget(uiBinder.createAndBindUi(this));
		// Inizializza il GFormBinder
		gFormBinder.createAndBindGForm(this);
	}

	@UiHandler("btnInsert")
	void onClickInsert(ClickEvent e) {
		comuneBinder.formToBean();
		comune.setIdComune(null);
		service.insert(comune, new GFormSuccessAsyncCallback<Comune>() {
			protected void onSuccessInternal(Comune result) {
				comune = result;
				comuneBinder.beanToForm();
			}
		});
	}

	@UiHandler("btnValidate")
	void onClickValidate(ClickEvent e) {
		validateAllField(new GValidationCallbackSuccess() {
			public void onValidationEnd() {
				Window.alert("OK");
			}
		});
	}

	@UiHandler("btnUpdate")
	void onClickUpdate(ClickEvent e) {
		comuneBinder.formToBean();
		
		service.update(comune, new GFormSuccessAsyncCallback<Comune>() {
			protected void onSuccessInternal(Comune result) {
				comune = result;
				comuneBinder.beanToForm();
			}
		});
	}

	@UiHandler("btnDelete")
	void onClickDelete(ClickEvent e) {
		service.delete(comune, new GFormSuccessAsyncCallback<Boolean>() {
			protected void onSuccessInternal(Boolean result) {
				Window.alert("Result:" + result);
			}
		});
	}

	
	@UiHandler("btnReset")
	void onClickReset(ClickEvent e) {
		reset();
	}
	
	@UiHandler("btnReload")
	void onClickRicarica(ClickEvent e)  {
		service.load(new Comune(2l), new GFormSuccessAsyncCallback<Comune>() {
			protected void onSuccessInternal(Comune result) {
				comune = result;
				comuneBinder.beanToForm();
			}
		});
	}
	
		
}
